package com.css.fxfzaqbz.modules.assessplot.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assessplot.entity.EvaluationResults;
import com.css.fxfzaqbz.modules.assessplot.repository.EvaluationResultsRepository;
import com.css.fxfzaqbz.modules.assessplot.service.EvaluationResultsService;
import com.css.fxfzaqbz.util.CreateGson;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformSessionUtils;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class EvaluationResultsServiceImpl implements EvaluationResultsService {
    @Resource
    private EvaluationResultsRepository evaluationResultsRepository;
    @Resource
    private SUserService sUserService;

    @Override
    public void save(EvaluationResults evaluationResults) {
        String userId = PlatformSessionUtils.getUserId();
        String userJson = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        evaluationResults.setCreateUser(userId);
        evaluationResults.setPeople(sUser.getUserName());
        evaluationResults.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        evaluationResults.setDelFlag(YNEnum.N.toString());
        //保存编号：KZ+时间到秒(KZ20230719103637)
        Date date = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyyMMddHHmmss");
        if ("0".equals(evaluationResults.getResult()))
            evaluationResults.setNumber("KZ" + sbf.format(date));
        if ("1".equals(evaluationResults.getResult()))
            evaluationResults.setNumber("PH" + sbf.format(date));
        if ("2".equals(evaluationResults.getResult()))
            evaluationResults.setNumber("SW" + sbf.format(date));
        if ("3".equals(evaluationResults.getResult()))
            evaluationResults.setNumber("DC" + sbf.format(date));
        evaluationResults.setIsShow("0");
        List<EvaluationResults> list = evaluationResultsRepository.getAll(evaluationResults);
        if (list.size() == 0) evaluationResults.setIsShow("1");
        evaluationResultsRepository.save(evaluationResults);
    }

    @Override
    public List<EvaluationResults> getAll(EvaluationResults evaluationResults) {
        return evaluationResultsRepository.getAll(evaluationResults);
    }

    @Override
    public List<EvaluationResults> getOne(String id) {
        return evaluationResultsRepository.getOne(id);
    }

    @Override
    public List<EvaluationResults> getById(String id) {
        return evaluationResultsRepository.getById(id);
    }

    @Override
    public void delete(String id) {
        evaluationResultsRepository.delete(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(EvaluationResults evaluationResults) {
        //把所有的都改成不展示状态
        evaluationResultsRepository.updateStatus2(evaluationResults);
        //把当前选择的这条改成相应的状态
        evaluationResultsRepository.updateStatus1(evaluationResults);


    }

    @Override
    public String getResult(String iaId) {
        return evaluationResultsRepository.getResult(iaId);
    }
}
